/**
 * vC[ʃCjVCY
 */
task Initialize_All(){
	//_~[p̃CjVCYJn
	Initialize_Dummy();
	
	//ʃf[^擾邽߂2F҂
	PWait( 2 );
	
	//OFTpʃf[^݂邩mF
	if( IsCommonDataAreaExists( "OFT" ) ){
		//[vpe|ϐ
		let tmp_Player = "";
		
		//ʃf[^݂
		while( Player == Player_Dummy ){
			tmp_Player = GetCommonDataDefaultEx( "OFT", "Player", "" );
			alternative( tmp_Player )
				case( "Sunny" ){
					Player = Player_Sunny;
				}
				case( "Luna" ){
					Player = Player_Luna;
				}
				case( "Star" ){
					Player = Player_Star;
				}
			
			yield;
		}
	}else{
		//ʃf[^݂Ȃ
		RaiseError( "̃vC[XNvgOriental Fairy's TalepłB\nOriental Fairy's TaleNĂB", "x" );
	}
	
	//IvC[̃CjVCYN
	alternative( Player )
		case( Player_Sunny ){
			Initialize_Sunny();
		}
		case( Player_Luna ){
			Initialize_Luna();
		}
		case( Player_Star ){
			Initialize_Star();
		}
	
	//vC[ʃC^XN
	TMain();
}

/**
 * vC[ʃC^XN
 */
task TMain(){
	let shieldcount = 0;
	let shieldmaxcount = 15;
	let shieldstartuse = 100;
	let shieldstartcount = 0;
	let recoverymaxcount = 80;
	let recoverycount = recoverymaxcount;
	let shotcount1 = 0;
	let shotcount2 = 0;
	let shotmaxcount2 = 20;
	let doublepush1 = 0;
	let doublepush2 = 0;
	let doublepush1count = 10;
	let doublepush2count = 10;
	let onshield = false;
	
	//{̃ACe^XNN
	CollectUserItemOnBomb();
	
	loop{
		//L[Xe[gi[
		GetKeyStateAll();
		
		//LȂƂ̂݊e푀
		if( true ){
			onshield = false;
			
			//[U[L[
			if( Key_User == KEY_PUSH ){
				if( shieldstartcount == 0 ){
					shieldcount = shieldmaxcount;
					shieldsize = 1;
					shieldstartcount = shieldmaxcount;
				}
			}else if( Key_User == KEY_HOLD ){
				if( shieldcount == 0 ){
					shieldcount += 1;
				}
			}
			
			//V[h or Q[W񕜏
			if( shieldcount > 0 ){
				//V[h
				onshield = Shield();
				shieldcount -= 1;
				recoverycount = 0;
				
				//V[hWJ̏p
				if( shieldstartcount > 0 ){
					SubShieldGauge( shieldstartuse / shieldmaxcount );
					shieldstartcount -= 1;
				}
			}else{
				//Q[W񕜗̉
				if( recoverycount < recoverymaxcount ){
					recoverycount += 1;
				}
				
				//Q[WR
				AddShieldGauge( GaugeRecovery * ( recoverycount / recoverymaxcount ) );
			}
			
			//V[hWJ摜TCYݒ
			if( shieldsize > 0 ){
				shieldangle += shieldangleplus;
				
				if( !onshield ){
					shieldsize -= 1 / shieldsizeframe;
				}
			}
			
			//[U[L[
			if( Key_Shot == KEY_PUSH ){
				doublepush1 = doublepush1count;
				//2񉟂 `[WVbg
				if( doublepush2 > 0 ){
					doublepush1 = 0;
					doublepush2 = 0;
					
					//`[WXgbN1ȏ゠Δ
					if( GetNowChargeCount() > 0 ){
						//`[WXgbN1炷
						SubNowChargeCount( 1 );
						//SEĐ
						PlaySE( SE_ChargeShot );
						
						//IvC[̃`[WVbgˏN
						alternative( Player )
							case( Player_Sunny ){
								ChargeShot_Sunny();
							}
							case( Player_Luna ){
								ChargeShot_Luna();
							}
							case( Player_Star ){
								ChargeShot_Star();
							}
					}
				}
			}else if( Key_Shot == KEY_PULL ){
				if( doublepush1 > 0 ){
					doublepush2 = doublepush2count;
				}
			}
			if( Key_Shot == KEY_PUSH || Key_Shot == KEY_HOLD ){
				shotcount2 = shotmaxcount2;
			}
			
			//Vbg
			if( doublepush1 > 0 ){
				doublepush1 -= 1;
			}
			if( doublepush2 > 0 ){
				doublepush2 -= 1;
			}
			if( shotcount2 > 0 ){
				shotcount2 -= 1;
				shotcount1 += 1;
				
				//IvC[̃VbgˏN
				alternative( Player )
					case( Player_Sunny ){
						Shot_Sunny( shotcount1 );
					}
					case( Player_Luna ){
						Shot_Luna( shotcount1 );
					}
					case( Player_Star ){
						Shot_Star( shotcount1 );
					}
			}
		}
		
		//1FEFCg
		PWait( 1 );
	}
}

/**
 * vC[ʃC[v
 */
function MainLoop_All(){
	//蔻ݒ
	if( intersectionCount == 0 ){
		SetIntersectionCircle( GetPlayerX(), GetPlayerY(), Intersection );
	}else{
		intersectionCount -= 1;
	}
}

/**
 * vC[ʕ`揈
 */
function DrawLoop_All(){
	//vC[̕`
	SetTexture( imgPlayer );
	SetRenderState( ALPHA );
	if( Key_Right == KEY_PUSH || Key_Right == KEY_HOLD ){
		//Eړ
		SetGraphicRect( Player_Rect * 2, Player_Rect * 0, Player_Rect * 3, Player_Rect * 1 );
	}else if( Key_Left == KEY_PUSH || Key_Left == KEY_HOLD ){
		//ړ
		SetGraphicRect( Player_Rect * 1, Player_Rect * 0, Player_Rect * 2, Player_Rect * 1 );
	}else{
		//ʏ
		SetGraphicRect( Player_Rect * 0, Player_Rect * 0, Player_Rect * 1, Player_Rect * 1 );
	}
	SetGraphicScale( 1, 1 );
	SetGraphicAngle( 0, 0, 0 );
	DrawGraphic( GetPlayerX(), GetPlayerY() );
	
	//V[h̕`
	let imgSize = 128;
	let imgIdx = 0;
	alternative( Player )
		case( Player_Sunny ){
			imgIdx = 0;
		}
		case( Player_Luna ){
			imgIdx = 3;
		}
		case( Player_Star ){
			imgIdx = 1;
		}
	SetTexture( imgShield );
	SetGraphicRect( imgSize * imgIdx, imgSize * 2, imgSize * (imgIdx + 1), imgSize * 3 );
	SetGraphicScale( shieldsize * ShieldRadius / ( imgSize / 2 ), shieldsize * ShieldRadius / ( imgSize / 2 ) );
	SetGraphicAngle( 0, 0, shieldangle );
	DrawGraphic( GetPlayerX(), GetPlayerY() );
}

/**
 * vC[ʃt@CiCY
 */
function Finalize_All(){
}

/**
 * vC[̐ݒꊇŃZbg
 */
function SetPlayerSettings(){
	//ړxݒ
	SetSpeed( HiSpeed, LowSpeed );
	
	//OCYaݒ
	SetGrazeCircle( Graze );
	
	//炢{Lt[ݒ
	SetRebirthFrame( Rebirth );
	
	//炢{t[ݒ
	SetRebirthFrameReduction( RebirthReduction );
	
	//{ݒ
	SetInitialBombCount( InitialBomb );
	
	//vC[c@摜ݒ
	SetPlayerLifeImage( imgPlayer, 0, 0, Player_Rect, Player_Rect );
	
	//ACeCݒ
	SetItemCollectLine( ItemCollect );
}

/**
 * V[h
 */
function Shield(){
	if( GetShieldGauge() > GaugeShieldUse ){
		SubShieldGauge( GaugeShieldUse );
		intersectionCount = 20;
		let bulletnum = GetEnemyShotCountEx( GetPlayerX(), GetPlayerY(), ShieldRadius, ALL );
		if( bulletnum > 0 ){
			AddChargeGauge( ChargePlus * bulletnum );
			AddScorePoint( bulletnum );
			AddBombExtendPoint( bulletnum );
		}
		DeleteEnemyShotImmediatelyInCircle( SHOT, GetPlayerX(), GetPlayerY(), ShieldRadius );
		
		return true;
	}else{
		return false;
	}
}

/**
 * ACe
 */
task CollectUserItem(){
	SetCommonDataEx( "OFT", "CollectUserItem", true );
	
	//Ô2F҂(vC[҂Ȃ)
	FWait( 2 );
	
	SetCommonDataEx( "OFT", "CollectUserItem", false );
}

/**
 * ACe
 */
task CollectUserItemOnBomb(){
	loop{
		while( !OnBomb() ){
			yield;
		}
		
		SetCommonDataEx( "OFT", "CollectUserItem", true );
		
		while( OnBomb() ){
			yield;
		}
		
		SetCommonDataEx( "OFT", "CollectUserItem", false );
	}
}
